import {
  getMachineInfo,
  getReviewInfo,
  deleteDevice,
  updateDevice
} from "@/components/device-info/device-info.service";
import {
  dateString,
  tractorTypeMappingObj,
  machineWorkTypeMappingObj,
  alertWithCallback,
  alertMessage
} from "@/utils/utils";
import { getUserInfo } from "@/utils/auth";

export default {
  components: {},
  props: {
    selectedData: {
      type: Object,
      default: () => {}
    },
    showDialog: {
      type: Boolean,
      default: false // 是否显示弹窗
    }
  },
  data() {
    return {
      userInfo: {},
      dateString: dateString,
      machineInfo: {},
      reviewInfo: {},
      deviceInfo: {},
      file_url: 'http://njgjapp.com:30065/wwwroot/'
    }
  },
  computed: {
    showDialogFlag: {
      get() {
        return this.showDialog
      },
      set(val) {
        this.$emit('update:showDialog', val)
      }
    }
  },
  created() {
    this.userInfo = getUserInfo();
    this.getMachineInfo();
    this.getReviewInfo();
  },
  methods: {
    getMachineInfo() {
      const params = {
        cid: this.selectedData.controllerid,
        ctp: this.selectedData.controllertype
      };
      getMachineInfo(params).then(res => {
        this.deviceInfo = res.Entity.Data
      });
    },

    getReviewInfo() {
      const params = {
        equipmentcode: this.selectedData.controllerid,
        enumequipmenttype: this.selectedData.controllertype
      };
      getReviewInfo(params).then(data => {
        if (data.Entity.Data !== null) {
          this.machineInfo = data.Entity.Data[0];
          const reviewInfo = JSON.parse(data.Entity.Data[0].registration);
          let cooperativeNames = reviewInfo.cooperativename.split('-');
          reviewInfo.area = data.Entity.Data[0].province + data.Entity.Data[0].city + data.Entity.Data[0].village + data.Entity.Data[0].prefecture;
          reviewInfo.cooperName = cooperativeNames[4] + '-' + cooperativeNames[5];
          reviewInfo.ownernameDisabled = true;
          reviewInfo.ownerphoneDisabled = true;
          reviewInfo.idcardDisabled = true;
          reviewInfo.bankcardDisabled = true;
          reviewInfo.facturerDisabled = true;
          reviewInfo.powerDisabled = true;
          reviewInfo.enumberDisabled = true;
          reviewInfo.enginenoDisabled = true;
          reviewInfo.framenoDisabled = true;
          reviewInfo.factorynoDisabled = true;
          reviewInfo.widthDisabled = true;
          reviewInfo.remarksDisabled = true;
          reviewInfo.registerDate = dateString(reviewInfo.applicationtime);
          reviewInfo.tractorTypeDes = tractorTypeMappingObj[reviewInfo.tractortype];
          reviewInfo.workTypeDes = machineWorkTypeMappingObj[reviewInfo.worktype] ? machineWorkTypeMappingObj[reviewInfo.worktype] : '免耕播种';
          this.reviewInfo = reviewInfo;
        }
      });
    },

    remove() {
      const note = '管理员账号' + this.userInfo.username + '删除了设备编码为' + this.reviewInfo.ttype + this.reviewInfo.tmcid + '的设备';
      alertWithCallback("确定要删除设备ID为" + this.reviewInfo.ttype + this.reviewInfo.tmcid + "的设备吗?", '提示').then(() => {
        const params = {
          Objectid: this.machineInfo.objectid,
          notes: note
        };
        deleteDevice(params).then(data => {
          if (data.StatusCode == 200) {
            this.showDialogFlag = false;
            alertMessage(data.Entity.Message, 3000);
          } else {
            alertMessage('删除失败, 设备未删除。服务器返回错误：' + '“' + data.Message + '”', 'warning');
          }
        })
      }).catch(() => {}); // 关闭confirm框的回调，不处理的话会报一个js error
    },

    updateInfo() {
      const note = '管理员账号' + this.userInfo.username + '修改了设备编码:' + this.reviewInfo.tmcid + '车主姓名:' + this.reviewInfo.ownername + ' 车牌号：' + this.reviewInfo.enumber + '的设备信息';
      const params = {
        model: {
          objectid: this.machineInfo.objectid,
          createtime: this.machineInfo.createtime,
          updatetime: this.machineInfo.updatetime,
          ename: this.machineInfo.ename,
          ecode: this.machineInfo.ecode,
          estatus: this.machineInfo.estatus,
          etype: this.machineInfo.etype,
          emodel: this.machineInfo.emodel,
          facturer: this.reviewInfo.facturer,
          province: this.machineInfo.province,
          city: this.machineInfo.city,
          village: this.machineInfo.village,
          prefecture: this.machineInfo.prefecture,
          enumber: this.reviewInfo.enumber,
          certificationtime: this.machineInfo.certificationtime,
          certificationstate: this.machineInfo.certificationstate,
          pkReviewerCustomer: this.machineInfo.pk_reviewer_customer,
          pkOwnerCustomer: this.machineInfo.pk_owner_customer,
          pkCooperative: this.machineInfo.pk_cooperative,
          registration: JSON.stringify(this.reviewInfo),
          factoryno: this.reviewInfo.factoryno
        },
        notes: note
      };
      alertWithCallback("确实要修改设备ID：" + this.reviewInfo.tmcid + ' 车主姓名：' + this.reviewInfo.ownername + ' 车主手机号：' + this.reviewInfo.ownerphone + ' 车牌号：' + this.reviewInfo.enumber + "的设备信息吗？", '提示').then(() => {
        updateDevice(params).then(data => {
          if (data.StatusCode == 200) {
            this.showDialogFlag = false;
            alertMessage(data.Entity.Message);
          } else {
            alertMessage('提交失败, 信息未修改。服务器返回错误：' + '“' + data.Message + '”', 'warning');
          }
        })
      }).catch(() => {}); // 关闭confirm框的回调，不处理的话会报一个js error
    }
  }
}
